﻿IF EXISTS (SELECT * FROM sysobjects WHERE type = 'P' AND name = 'P_Rpt_UsersOnline')
	BEGIN
		DROP  Procedure  [esf_sso].P_Rpt_UsersOnline
	END

GO


CREATE Procedure [esf_sso].P_Rpt_UsersOnline
AS

	SELECT
		X.*
	FROM
		(SELECT
			VU.Usr_Alias
			,VU.Usr_Per_cal_FullName
			,VU.Usr_Per_Ent_Name
			,VU.Usr_Per_Email
			,VU.Usr_Per_IT_Code
			,VU.Usr_Per_IdentificationNumber
			,MAX (USL.StartDateTime) Usr_LastStartDateTime
			,MAX (USL.UserHostAddress) Usr_LastUserHostAddress
		FROM 
			esf_sso.VUser VU
		INNER JOIN
			esf_sso.UserSessionLog USL ON
				USL.UserPersonID = VU.Usr_Per_ID
				AND USL.EndDateTime IS NULL 
				AND USL.StartDateTime > DATEADD (d, -1, GETDATE ())
		GROUP BY
			VU.Usr_Alias
			,VU.Usr_Per_cal_FullName
			,VU.Usr_Per_Ent_Name
			,VU.Usr_Per_Email
			,VU.Usr_Per_IT_Code
			,VU.Usr_Per_IdentificationNumber) X
	ORDER BY
		X.Usr_LastStartDateTime DESC